package com.labot.webapp.action;

import java.util.Date;
import java.util.List;
import java.util.Set;

import com.labot.model.Area;
import com.labot.model.Envio;


/**
 * Action for facilitating User Management feature.
 */
public class AreaAction extends BaseAction {
    private static final long serialVersionUID = 6776548938712115191L;
    
    private Set<Area> areas;
   
    private String idarea;
    private String nombre;
    private String mail;

    public String execute() {
        return SUCCESS;
    }
    
    public String cancel() {
    	return CANCEL;
    }


    /**
     * Fetch all Configs from database and put into local "configs" variable for retrieval in the UI.
     *
     * @return "success" if no exceptions thrown
     */
    public String list() {
    		
    	areas = areaManager.getAreas();
    	
    	return SUCCESS;
    }
    
    public String edit(){
    	if(idarea != null && !idarea.equals("")){
    		Area a = areaManager.buscarArea(Long.parseLong(idarea));
    		nombre = a.getNombre();
    		mail = a.getMail();
    	}
    	return SUCCESS;
    }
    
    public String save(){
    	Area a = null;
    	if(idarea != null && !idarea.equals("")){
    		a = areaManager.buscarArea(Long.parseLong(idarea));
    	}else{
    		a = new Area();
    	}
    	a.setNombre(nombre);
    	a.setMail(mail);
    	try{
    	areaManager.save(a);
    	}catch(Exception e){
    		super.addActionError("Error al cargar el area!");
    		e.printStackTrace();
    		return ERROR;
    	}
    	super.addActionMessage("Area cargada correctamente!");
    	return SUCCESS;
    	
    }
    
    public String remove(){
    	try{
    		Area a = areaManager.buscarArea(Long.parseLong(idarea));
    		a.delete();
    		areaManager.save(a);
    	}catch(Exception e){
    		super.addActionError("Error al borrar el area!");
    		return SUCCESS;
    	}
    	super.addActionMessage("Se ha borrado el area!");
    	return SUCCESS;
    	
    }

	public Set<Area> getAreas() {
		return areas;
	}

	public void setAreas(Set<Area> areas) {
		this.areas = areas;
	}

	public String getIdarea() {
		return idarea;
	}

	public void setIdarea(String idarea) {
		this.idarea = idarea;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}
    
	
	
	
	

	
	

}
